package com.suifeng.card.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.suifeng.card.api.common.Result;
import com.suifeng.card.entity.Assets;
import com.suifeng.card.entity.User;
import com.suifeng.card.service.FileService;
import com.suifeng.card.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * (User)表控制层
 *
 */
@RestController
@Slf4j
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    @Resource
    private FileService fileService;

    @Value("${storage.service.type}")
    private String storageType;

    /**
     * 通过username查询单条数据
     * @param user 实体
     * @return
     */
    @PostMapping("/no-auth/info")
    public Result<User> queryByuserName(@RequestBody User user) {
        try {
            if (log.isInfoEnabled()) {
                log.info("新增用户入参{}", JSON.toJSONString(user));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(user.getUsername()), "用户名不能为空");
            return Result.ok(this.userService.queryByuserName(user));
        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error("查询单条数据异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 注册用户
     *
     * @param user 实体
     * @return 新增结果
     */
    @PostMapping("/no-auth/register")
    public Result<Boolean> add(@RequestBody User user) {
        try {
            if (log.isInfoEnabled()) {
                log.info("新增用户入参{}", JSON.toJSONString(user));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(user.getUsername()), "用户名不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(user.getPassword()), "密码不能为空");
            return Result.ok(this.userService.insert(user));
        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error("新增用户异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 发送邮箱验证码
     */
    @PostMapping("/email/send")
    public Result<Boolean> sendEmail(@RequestBody User user) {
        try {
            if (log.isInfoEnabled()) {
                log.info("发送邮箱验证码{}", JSON.toJSONString(user));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(user.getEmail()), "邮箱不能为空");
            return Result.ok(this.userService.sendEmail(user));
        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error("发送邮箱验证码！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }
    /**
     * 获取邮箱验证码(用于校验用户输入)
     */
    @PostMapping("/auth/captcha")
    public Result<String> getEmailcaptcha(@RequestBody User user) {
        try {
            if (log.isInfoEnabled()) {
                log.info("获取邮箱验证码{}", JSON.toJSONString(user));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(user.getEmail()), "邮箱不能为空");
            return Result.ok(this.userService.getEmailcaptcha(user));
        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error("获取邮箱验证码！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }
    /**
     * 上传文件
     */
    @PostMapping("/user/uploadFile")
    public Result<String> uploadFile(MultipartFile uploadFile) {
        try {
            return Result.ok(this.fileService.uploadFile(uploadFile,"card","card"));
        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error("发送邮箱验证码错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }
    /**
     * 删除oss指定文件
     */
    @DeleteMapping("/user/deletefile")
    public Result deletefile(@RequestBody Assets assets) {
        try {
            this.fileService.deletefile(assets);
            return Result.ok();
        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error("删除oss指定文件错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }
    /**
     * 用户名登录
     */
    @PostMapping("/auth/login")
    public Result<SaResult> ulogin(@RequestBody User user) {
        try {
            Preconditions.checkArgument(!StringUtils.isBlank(user.getUsername()), "用户名不能为空");
            return Result.ok( this.userService.ulogin(user));
        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error("用户名登录错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }
    /**
     * 邮件登录
     */
    @PostMapping("/no-auth/email-login")
    public Result<SaResult> elogin(@RequestBody User user) {
        try {
            Preconditions.checkArgument(!StringUtils.isBlank(user.getEmail()), "邮件名不能为空");
            return Result.ok( this.userService.elogin(user));
        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error("邮件名登录错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }
    /**
     * 退出登录
     */
    @PostMapping("/auth/exit-login")
    public Result exitlogin(@RequestBody User user) {
        try {
            log.info("UserController.exitlogin.userName:{}", user.getUsername());
            Preconditions.checkArgument(!StringUtils.isBlank(user.getUsername()), "用户名不能为空");
            StpUtil.logout(user.getUsername());
            return Result.ok();
        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error("邮件名登录错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }
}

